/**
* @program: Go
*
* @description:消息逻辑类
*
* @author: Mr.chen
*
* @create: 2020-12-01 09:53
**/
package message

import (
	"ydd-im/service/api/models"
	"fmt"

	mydb "ydd-im/service/api/tools/mysql/conn"
	"ydd-im/service/api/tools/util"
	"errors"

	"time"
)

// 添加message消息记录以及未到达记录表
func AddMessage(TouserIds[]int, userid, messageType, dstid, media int, content, pic, url, memo string,createTime time.Time) (restIdint int,err error) {
	dbconn,err := mydb.DBConn("w")
	if err != nil {
		return 0, err
	}
	tx, err := dbconn.Beginx() // 开启事务
	if err != nil {
		return 0,err
	}
	// 插入消息表数据库
	message := &models.Message{
		Userid:      userid,
		MessageType: messageType,
		Dstid:       dstid,
		Media:       media,
		Content:     content,
		Pic:         pic,
		Url:         url,
		Memo:        memo,
		Createtime:  createTime,
	}
	restId, err := new(models.Message).AddMessage(message)
	if err != nil {
		tx.Rollback()
		return 0,errors.New("插入消息记录表失败")
	}
	var MessageArrive []*models.MessageArrive
	restIdint = util.Int64Toint(restId)
	for k,_ := range TouserIds {
		messaged := &models.MessageArrive{
			MessageId:  restIdint,
			Dstid:TouserIds[k],
			Createtime:createTime,
		}
		MessageArrive = append(MessageArrive,messaged)
	}
	//messaged := &models.MessageArrive{
	//	MessageId:  restIdint,
	//	Dstid:dstid,
	//}
	//messageu := &models.MessageArrive{
	//	MessageId: restIdint,
	//	Dstid:userid,
	//}
	_, err = new(models.MessageArrive).BatchInsertMessageArrive(MessageArrive)
	if err != nil {
		tx.Rollback()
		return 0,errors.New("插入消息未到达记录表失败")
	}
	tx.Commit()
	return restIdint,nil
}

// 绑定userid与客户端clientid到user_clients表
func AddClientToUser(userid int, clientId string) (err error) {
	//校验是否为空
	if userid <= 0 || len(clientId) == 0 {
		return errors.New("userid,clientid不能为空")
	}
	// 查询数据库是否存在
	userToClient := &models.UserClients{ClientId: clientId, Userid: userid, Creattime: time.Now()}
	modeluserToClient := new(models.UserClients)
	_, err = modeluserToClient.AddUserClients(userToClient)
	if err != nil {
		return errors.New("保存clientId失败")
	}
	return nil
}
// 删除messageArrive
func DeletemessageArrive(messageId int,dstid int) (err error) {
	//校验是否为空
	if  messageId <=0 ||  dstid <= 0 {
		return errors.New("messageId,dstid参数不合法")
	}
	// 查询数据库是否存在
	messageArrive := new(models.MessageArrive)
	err =messageArrive.DeleteRowMessageArrive(messageId,dstid)
	if err != nil {
		return errors.New("删除消息失败")
	}
	return nil
}
// 更新messageArrive状态
func UpdatemessageArrive(messageId int,dstid int,state int) (err error) {
	//校验是否为空
	if  messageId <=0 ||  dstid <= 0 ||  state <= 0 {
		return errors.New("messageId,dstid,state参数不合法")
	}
	// 查询数据库是否存在
	messageArrive := new(models.MessageArrive)
	err =messageArrive.UpdateRowMessageArrive(messageId,dstid,state)
	if err != nil {
		return errors.New("更新消息状态失败")
	}
	return nil
}

// 获取messageArrive列表
func GetMessageArriveJoinMessageList(userid int) (message []models.Message,err error) {
	//校验是否为空
	if  userid <=0  {
		return message,errors.New("userid参数不合法")
	}
	// 查询数据库是否存在
	messageArrive := new(models.MessageArrive)
	message,err = messageArrive.GetMessageArriveJoinMessageList(userid)
	fmt.Println(message)
	if err != nil {
		return message,err
	}
	return message,nil
}

// 获取messageArrive列表
func GetMessageHistoryLogList(messageType int,dstid int ) (message []models.Message,err error) {
	//校验是否为空
	if  messageType < 0  || dstid <= 0{
		return message,errors.New("参数不合法")
	}
	if messageType>0 {
		messageType = 1
	}else{
		messageType = 0
	}
	// 查询数据库是否存在
	var where []string
	if messageType >= 0 {
		where = append(where, fmt.Sprintf("b.messageType = %d",messageType))
	}
	if dstid > 0 {
		where = append(where, fmt.Sprintf("a.dstid = %d",dstid))
	}
	field := "b.id,b.userid,b.messageType,b.dstid,a.dstid as toUserId,b.media,b.content,b.pic,b.url,DATE_FORMAT(b.createtime, '%Y-%m-%d %T') as createtimestr,c.username"
	modelMessage := new(models.MessageArrive)
	message,err = modelMessage.GetMessageHistoryLogList(where,field,0,0,"")
	if err != nil {
		return message,err
	}
	return message,nil
}